package org.dkakunsi.aplikasitoko.form;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import org.dkakunsi.aplikasitoko.data.entity.Kasir;
import org.dkakunsi.aplikasitoko.data.entity.Log;
import org.dkakunsi.aplikasitoko.data.util.ApplicationDataException;
import org.dkakunsi.aplikasitoko.data.util.Generator;
import org.dkakunsi.aplikasitoko.service.DataService;

public class Login extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JLabel lblUser;
	private JTextField textField;
	private JPasswordField passwordField;
	private JButton btnLogin;
	private JButton btnExit;
	private DataService ds;


	/**
	 * Create the frame.
	 */
	public Login() {
		super();
		getContentPane().setLayout(null);
		setUndecorated(true);
		setBounds(100, 100, 450, 300);
		
		lblUser = new JLabel("User Name");
		lblUser.setBounds(72, 124, 100, 25);
		getContentPane().add(lblUser);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(72, 163, 70, 20);
		getContentPane().add(lblPassword);
		
		textField = new JTextField();
		textField.setBounds(204, 124, 172, 25);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(204, 161, 172, 25);
		getContentPane().add(passwordField);
		
		btnLogin = new JButton("LOGIN");
		btnLogin.setBounds(299, 216, 77, 25);
		getContentPane().add(btnLogin);
		btnLogin.setActionCommand("LOGIN");
		btnLogin.addActionListener(this);
		
		btnExit = new JButton("EXIT");
		btnExit.setBounds(203, 216, 77, 25);
		getContentPane().add(btnExit);
		btnExit.setActionCommand("EXIT");
		btnExit.addActionListener(this);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		ds = new DataService();
		
		if(command.equals("LOGIN")){
			String UNAME = textField.getText();
			String PASS = passwordField.getText();
			
			if(UNAME.equals("ADMIN") && PASS.equals("ADMIN")){
				dispose();
				new Desktop("ADMIN", null).show(true);
			} else if(UNAME.equals("GUDANG") && PASS.equals("GUDANG")){
				dispose();
				new Desktop("GUDANG", null).show(true);
			} else {
				Kasir kasir = null;
				try {
					kasir = (Kasir) ds.getInstance(UNAME, "KASIR");
				} catch (ApplicationDataException e1) {
					JOptionPane.showMessageDialog(this, "USERNAME dan/atau PASSWORD anda salah", "Kesalahan Otentikasi", JOptionPane.ERROR_MESSAGE);
//					e1.printStackTrace();
				}
				
				if(kasir!=null){
					if(kasir.getPass().equals(PASS)){
						dispose();

						Log log = createLog();
						ds.attach(kasir);
						kasir.addLog(log);
						ds.stop();
						try {
							ds.save(log);
						} catch (ApplicationDataException e1) {
							JOptionPane.showMessageDialog(this, "Call the Developer", "Application Error", JOptionPane.ERROR_MESSAGE);
//							e1.printStackTrace();
						}
						new Desktop("KASIR", log).show(true);
					} else {
						JOptionPane.showMessageDialog(this, "USERNAME dan/atau PASSWORD anda salah", "Kesalahan Otentikasi", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		} else {
			System.exit(0);
		}
	}
	
	private Log createLog(){
		Log log = new Log();
		log.setTanggal(Generator.getDateNow());
		log.setJamMasuk("");
		log.setJamKeluar("");
		
		return log;
	}

}
